package com.zy.web.controllers;

import com.zy.service.BannerService;
import com.zy.web.bean.BannerDTO;
import com.zy.web.bean.ResponseResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: shopping153_cloud
 * @description: 广告轮播图片的控制器
 * @author: zy
 * @create: 2025-08-21 08:47
 */
@RestController
@RequestMapping("/banner")
@Tag(name="广告轮播图片的控制器",description = "提供对轮播图资源的添加,查询并显示等操作")
@RefreshScope
@Log
public class BannerController {

    //private final BannerService bannerService;
   // public BannerController(BannerService bannerService) {
   //     this.bannerService = bannerService;
   // }

    @Autowired
    private BannerService bannerService;

    @Value("${product.pattern.dateFormat}")
    private String dateFormat;

    /**
     * 根据状态查询轮播图
     * GET /banner/status/{bannerStatus}
     * @param bannerStatus 轮播图状态: 要求三个值: -1所有, 0隐藏, 1显示
     * @return 包含轮播图DTO列表的统一响应
     */
    @GetMapping("/status/{bannerStatus}")
    public ResponseEntity<ResponseResult> getBannersByStatus(
            @PathVariable( value="bannerStatus", required = true) Integer bannerStatus,
            @RequestHeader( value="token", required = false) String token) {

        log.info("获取到了filter中的 token："+token);

        List<BannerDTO> banners = bannerService.getBannersByStatus(bannerStatus);

        return ResponseEntity.ok(ResponseResult.ok("查询轮播图成功").setData(banners));
    }


    @PostMapping("/add")
    @RefreshScope
    public ResponseEntity<ResponseResult<BannerDTO>> addBanner(   BannerDTO bannerDTO  ){

        DateFormat df=new SimpleDateFormat(   dateFormat );
        System.out.println("今天是:"+df.format(new Date()));


        bannerDTO = bannerService.addBanner(bannerDTO);
        return ResponseEntity.ok(   ResponseResult.ok().setData(   bannerDTO)     );
    }
}
